package com.thinvent.smart.chain.controller;

import com.alipay.mychain.sdk.vm.EVMOutput;
import com.alipay.mychain.sdk.vm.EVMParameter;
import com.thinvent.smart.chain.common.BaseCommon;
import com.thinvent.smart.chain.common.CallChain;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@RequestMapping("/testbank")
@RestController
public class TestBankController {
    /**
     * 获取银行卡余额
     */
    @GetMapping("/getBankAccount")
    public Integer getBankAccount(@RequestParam("accountId") String accountId
            , BaseCommon baseCommon) throws IOException {
        EVMParameter parameters = new EVMParameter("getBankAccount(string)");
        parameters.addString(accountId);
        CallChain chain = new CallChain(baseCommon.getAccount(),baseCommon.getUserPassword());
        EVMOutput evmOutput = chain.testContractId(baseCommon.getTestContractId()).callContract(parameters);
        System.out.println( "输出："+evmOutput.getString());
        return evmOutput.getInt().intValue();
    }

    @GetMapping("/updateContract")
    public boolean updateContract(@RequestParam("contractCode") String contractCode
            , BaseCommon baseCommon) throws IOException {
        CallChain chain = new CallChain(baseCommon.getAccount(),baseCommon.getUserPassword());
        boolean b = chain
                .testContractId(baseCommon.getTestContractId())
                .contractCode(contractCode)
                .updateContract();
        return b;
    }
    @GetMapping("/deployContract")
    public boolean deployContract(@RequestParam("contractCode") String contractCode
            , BaseCommon baseCommon) throws IOException {
        EVMParameter contractParameters = new EVMParameter();

        CallChain chain = new CallChain(baseCommon.getAccount(),baseCommon.getUserPassword());
        boolean b = chain
                .testContractId(baseCommon.getTestContractId())
                .contractCode(contractCode)
                .deployContract(contractParameters);
        return b;
    }
}
